package cn.android666.timbertree

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import cn.android666.timbertree.simple.R
import cn.android666.timbertree.simple.databinding.ActivityMainBinding
import timber.log.Timber

class MainActivity : BaseActivity() {

    private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
    private var name: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        binding.makeLogButton.setOnClickListener {
            // verbose和debug级别的日志：打印到控制台
            Timber.v("verbose")
            Timber.d("debug")

            // info和warning级别的日志：打印到控制台 + 写到日志文件
            Timber.i("info")
            Timber.w("warning")

            // error和wtf级别的日志：打印到控制台 + 写到日志文件 + 写到异常文件
            Timber.e(Exception("I'm sorry!"))
            Timber.e("error")
            Timber.wtf("wtf")

            // 获取日志文件目录，方便上传日志时使用
            val fileTree = Timber.forest().find { it is FileTree } as FileTree
            Timber.i("logDir=${fileTree.logFilesDir}")
            Timber.i("bugDir=${fileTree.bugFilesDir}")
        }

        binding.makeBugButton.setOnClickListener {
            // 模拟一个空指针异常
            val name = this.name!!
            Timber.i("name=$name")
        }
    }

}